package com.ecoolex.ying.space.core.manager;

import com.ecoolex.ying.space.core.dao.UserWorksStatisticalDAO;
import com.ecoolex.ying.space.entity.UserWorksStatistical;

import java.sql.Date;
import java.time.Instant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserWorksStatisticalManager {
	
    @Autowired
    private UserWorksStatisticalDAO dao;
    
    public UserWorksStatistical getByWorksId(Integer worksId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(worksId, UserWorksStatistical.NOT_DELETED);
    }
    
    public void add(Integer worksId,String createUser) {
    	UserWorksStatistical userWorksStatistical = new UserWorksStatistical();
    	userWorksStatistical.setWorksId(worksId);
    	userWorksStatistical.setPraseCount(0);
    	userWorksStatistical.setCommentCount(0);
    	userWorksStatistical.setFavoriteCount(0);
    	userWorksStatistical.setDeleted(UserWorksStatistical.NOT_DELETED);
    	userWorksStatistical.setCreateUser(createUser);
    	userWorksStatistical.setCreateTime(Date.from(Instant.now()));
    	userWorksStatistical.setUpdateUser(createUser);
    	userWorksStatistical.setUpdateTime(Date.from(Instant.now()));

		dao.insert(userWorksStatistical);
    }
    
    /**
     * 评论计数
     * @param worksId
     */
    public void commentCount(Integer worksId,String updateUser) {
    	dao.commentCount(worksId, updateUser);
    }
    
    /**
     * 删除评论计数
     * @param worksId
     */
    public void delCommentCount(Integer worksId,String updateUser) {
    	dao.delCommentCount(worksId, updateUser);
    }
    
    /**
     * 点赞计数
     * @param worksId
     */
    public boolean praseCount(Integer worksId,String updateUser) {
    	return dao.praseCount(worksId, updateUser)==1;
    }
    
    /**
     * 取消点赞计数
     * @param worksId
     */
    public boolean cancelPraseCount(Integer worksId,String updateUser) {
    	return dao.cancelPraseCount(worksId, updateUser)==1;
    }
    
    /**
     * 收藏计数
     * @param worksId
     */
    public void collectCount(Integer worksId,String updateUser) {
    	dao.collectCount(worksId, updateUser);
    }
    
    /**
     * 取消收藏计数
     * @param worksId
     */
    public void cancelCollectCount(Integer worksId,String updateUser) {
    	dao.cancelCollectCount(worksId, updateUser);
    }
}